package com.axjy.quartz.client;

import com.axjy.comm.client.QuartzClient;
import com.axjy.comm.vo.JobAndTrigger;
import com.axjy.quartz.mapper.QrtzTriggersMapper;
import com.axjy.quartz.pojo.QrtzTriggers;
import lombok.SneakyThrows;
import org.quartz.*;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class QuartzClientImpl implements QuartzClient {

    @Resource
    private Scheduler scheduler;

    @Resource
    private QrtzTriggersMapper qrtzTriggersMapper;

    @SneakyThrows
    @Override
    public void add(JobAndTrigger jobAndTrigger) {
        Class clazz=Class.forName(jobAndTrigger.getJobClassName());
        JobDetail jobDetail= JobBuilder.newJob(clazz)
                .storeDurably()
                .withIdentity(jobAndTrigger.getJobName(),jobAndTrigger.getJobGroup())
                .withDescription(jobAndTrigger.getDescription())
                .build();
        TriggerBuilder triggerBuilder=TriggerBuilder.newTrigger()
                .withIdentity(jobAndTrigger.getTriggerName(),jobAndTrigger.getTriggerGroup())
                .withDescription(jobAndTrigger.getDescription());
        if(StringUtils.hasLength(jobAndTrigger.getCronExpression())){
            CronScheduleBuilder cronScheduleBuilder=CronScheduleBuilder.cronSchedule(jobAndTrigger.getCronExpression());
            triggerBuilder.withSchedule(cronScheduleBuilder);
        }else{
            SimpleScheduleBuilder scheduleBuilder=SimpleScheduleBuilder.simpleSchedule()
                    .withIntervalInSeconds(jobAndTrigger.getRepeatInterval())
                    .withRepeatCount(jobAndTrigger.getCount());
            triggerBuilder.withSchedule(scheduleBuilder);
        }
        scheduler.scheduleJob(jobDetail,triggerBuilder.build());
    }

    @SneakyThrows
    @Override
    public void del(JobAndTrigger jobAndTrigger) {
        JobKey jobKey=JobKey.jobKey(jobAndTrigger.getJobName(),jobAndTrigger.getJobGroup());
        TriggerKey triggerKey=TriggerKey.triggerKey(jobAndTrigger.getTriggerName(),jobAndTrigger.getTriggerGroup());
        scheduler.unscheduleJob(triggerKey);
        scheduler.deleteJob(jobKey);
    }

    @SneakyThrows
    @Override
    public void update(JobAndTrigger jobAndTrigger) {
        TriggerKey triggerKey=TriggerKey.triggerKey(jobAndTrigger.getTriggerName(),jobAndTrigger.getTriggerGroup());
        TriggerBuilder triggerBuilder=TriggerBuilder.newTrigger()
                .withIdentity(jobAndTrigger.getTriggerName(),jobAndTrigger.getTriggerGroup())
                .withDescription(jobAndTrigger.getDescription());
        if(StringUtils.hasLength(jobAndTrigger.getCronExpression())){
            CronScheduleBuilder cronScheduleBuilder=CronScheduleBuilder.cronSchedule(jobAndTrigger.getCronExpression());
            triggerBuilder.withSchedule(cronScheduleBuilder);
        }else{
            SimpleScheduleBuilder scheduleBuilder=SimpleScheduleBuilder.simpleSchedule()
                    .withIntervalInSeconds(jobAndTrigger.getRepeatInterval())
                    .withRepeatCount(jobAndTrigger.getCount());
            triggerBuilder.withSchedule(scheduleBuilder);
        }
        Trigger trigger=triggerBuilder.build();
        scheduler.rescheduleJob(triggerKey,trigger);
    }

    private JobAndTrigger transfer(QrtzTriggers qrtzTriggers){
        JobAndTrigger jobAndTrigger=new JobAndTrigger();
        /*jobAndTrigger.setJobName(qrtzTriggers.getJobName());
        jobAndTrigger.setTriggerName(qrtzTriggers.getTriggerName());
        jobAndTrigger.setJobGroup(qrtzTriggers.getJobGroup());
        jobAndTrigger.setTriggerGroup(qrtzTriggers.getTriggerGroup());
        jobAndTrigger.setDescription(qrtzTriggers.getDescription());*/
        BeanUtils.copyProperties(qrtzTriggers,jobAndTrigger);
        return jobAndTrigger;
    }

    @Override
    public List<JobAndTrigger> searchAll() {
        List<QrtzTriggers> list=qrtzTriggersMapper.selectList(null);
        List<JobAndTrigger> list2= list.stream().map(this::transfer).collect(Collectors.toList());
        return list2;
    }

    @SneakyThrows
    @Override
    public void pause(JobAndTrigger jobAndTrigger) {
        JobKey jobKey=JobKey.jobKey(jobAndTrigger.getJobName(),jobAndTrigger.getJobGroup());
        TriggerKey triggerKey=TriggerKey.triggerKey(jobAndTrigger.getTriggerName(),jobAndTrigger.getTriggerGroup());
        scheduler.pauseJob(jobKey);
        scheduler.pauseTrigger(triggerKey);
    }

    @SneakyThrows
    @Override
    public void resume(JobAndTrigger jobAndTrigger) {
        JobKey jobKey=JobKey.jobKey(jobAndTrigger.getJobName(),jobAndTrigger.getJobGroup());
        TriggerKey triggerKey=TriggerKey.triggerKey(jobAndTrigger.getTriggerName(),jobAndTrigger.getTriggerGroup());
        scheduler.resumeJob(jobKey);
        scheduler.resumeTrigger(triggerKey);
    }

}
